package com.changgou.search.controller;

import com.changgou.entity.Page;
import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/sku_search")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @GetMapping
    @ResponseBody
    public Map search(@RequestParam Map<String,String> paramMap){
        //特殊符号的处理
        handlerParamMap(paramMap);
        //关键字查询
        Map resultMap = searchService.search(paramMap);
        return resultMap;
    }

    private void handlerParamMap(Map<String, String> paramMap) {
        //将特殊字符进行转换,由于操作系统,请求的+,变成空格.
        //%2B 就是 + 的意思
        Set<Map.Entry<String, String>> entries = paramMap.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            //spec_里面的值才会包含+号
            if(entry.getKey().startsWith("spec_")){
                // spec_版本=6GB+128GB --> spec_版本=6GB 128GB
                // spec_版本=6GB 128GB --> spec_版本=6GB%2B128GB
                System.out.println("转换前 : "+entry.getValue());
                String value = entry.getValue().replace(" ", "%2B");//将空格替换成%2B
                paramMap.put(entry.getKey(),value);
                System.out.println("转换后 : "+entry.getValue());
//                entry.getValue().replace("+","%2B"); //将空格替换成%2B
            }
        }
    }

    @GetMapping("/list")
    public String search(@RequestParam Map<String,String> paramMap, Model model){

        //获取页面结果
        Map resultMap = search(paramMap);

        //将数据存入到model中,进行页面跳转
        //页面的参数,查询/过滤条件
        model.addAttribute("paramMap",paramMap);
        model.addAttribute("resultMap",resultMap);

        //http://localhost:9009/sku_search/list?key=value&key=value...
        //拼接url参数,点击某个<a>标签.能够访问该方法,实现查询或条件过滤功能
        StringBuilder baseUrl = new StringBuilder("/sku_search/list");
        if(paramMap != null && paramMap.size() > 0) {
            baseUrl.append("?");
            //遍历参数集合,进行参数条件拼接
            for (Map.Entry<String, String> entry : paramMap.entrySet()) {
                if (entry != null && !"pageNum".equals(entry.getKey()) && !"sortField".equals(entry.getKey()) && !"sortRule".equals(entry.getKey())) {
                    //参数拼接
                    //http://localhost:9009/sku_search/list?key=value&key=value&
                    baseUrl.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
                }
            }
            String url = baseUrl.toString();
            //http://localhost:9009/sku_search/list?key=value&key=value
            url = url.substring(0, url.length() - 1);
            System.out.println("url : "+url);
            model.addAttribute("url",url);
        }else{
            model.addAttribute("url",baseUrl);
        }

        //分页参数的设置
        //参数1,总记录数,参数2,当前页,参数3,每页条数.
        Long total = (Long) resultMap.get("total");
        Integer pageSize = (Integer) resultMap.get("pageSize");
        String pageNum = (String) resultMap.get("pageNum");

        Page page = new Page(
                total,Integer.parseInt(pageNum),pageSize
        );

        model.addAttribute("page",page);

        return "search";
    }

}
